package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func ConnectMongoDb() *mongo.Client {
	configMap := InitConfig("./" + configFileName)
	uri := configMap["mongodbconnetstr"]
	option := options.Client().ApplyURI(uri).SetConnectTimeout(2 * time.Second)
	client, err := mongo.Connect(context.Background(), option)
	if err != nil {
		log.Fatal(err.Error())
	}
	err = client.Ping(context.Background(), nil) //ping通才代表连接成功
	if err != nil {
		log.Fatal(err.Error())
	}
	//collection := client.Database("users").Collection("users_all") //选择对应集合
	return client
	//create(context.Background(), collection)
	//delete(context.TODO(), collection)
}
func create(ctx context.Context, collection *mongo.Collection, username string, password string) bool {
	doc := User{Name: username, Password: password, Role: 1}
	if Checkuser(ctx, collection, username, password, false) {
		return false
	}
	res, err := collection.InsertOne(ctx, doc)
	if err != nil {
		log.Fatal(err.Error())
		return false
	}

	fmt.Printf("insert id %v \n", res.InsertedID)
	return true
	// docs := []interface{}{User{Name: "李四", Age: 15, Score: 50}}
	// ress, err := collection.InsertMany(ctx, docs)
	// if err != nil {
	// 	// handle error
	// }
	// fmt.Printf("insert many ids:%v\n", ress.InsertedIDs)
}
func Checkuser(ctx context.Context, collection *mongo.Collection, username string, password string, checkpassword bool) bool {

	sort := bson.D{{Key: "name", Value: 1}} //1为升序
	filter := bson.D{{Key: "name", Value: username}}
	if checkpassword {
		filter = bson.D{{Key: "name", Value: username}, {Key: "password", Value: password}}
	}
	findOption := options.Find()
	findOption.SetSort(sort)
	findOption.SetLimit(10)
	//findOption.SetSkip(1)
	cursor, err := collection.Find(ctx, filter, findOption)
	if err != nil {
		log.Fatal(err)
	}

	for cursor.Next(ctx) {
		var doc User
		err := cursor.Decode(&doc)
		if err != nil {
			log.Fatal(err)
		}
		//fmt.Printf("%s %d %d\n", doc.Name, doc.Age, doc.Score)
	}
	return cursor.Current != nil
}
func findallusera(ctx context.Context, collection *mongo.Collection) []User {
	sort := bson.D{{Key: "name", Value: 1}} //1为升序
	filter := bson.D{{}}
	findOption := options.Find()
	findOption.SetSort(sort)
	//findOption.SetLimit(10)
	//findOption.SetSkip(1)
	cursor, err := collection.Find(ctx, filter, findOption)
	if err != nil {
		log.Fatal(err)
	}
	personList := []User{}

	for cursor.Next(ctx) {
		var doc User
		err := cursor.Decode(&doc)
		if err != nil {
			log.Fatal(err)
		}
		personList = append(personList, doc)
		//fmt.Printf("%s %d %d\n", doc.Name, doc.Age, doc.Score)
	}
	return personList
}
func update(ctx context.Context, collection *mongo.Collection, name string) {
	filter := bson.D{{Key: "name", Value: name}}
	update := bson.D{{Key: "$set", Value: bson.D{{Key: "name", Value: name}}}}
	res, err := collection.UpdateMany(ctx, filter, update)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("update %d doc \n", res.ModifiedCount)
}
func delete(ctx context.Context, collection *mongo.Collection, name string) {
	filter := bson.D{{Key: "name", Value: name}}
	res, err := collection.DeleteMany(ctx, filter)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("delete %d doc \n", res.DeletedCount)
}
